import { defineStore } from 'pinia';
import { nanoid } from 'nanoid';
import axios from 'axios';

/*export const useTalkStore = defineStore("talk", {
  state() {
    return {
      // 读取本地存储，localStorage存储的其中一特点就是关掉浏览器也不会丢失
      talkList: JSON.parse(localStorage.getItem("talkList") as string) || [],
    };
  },
  actions: {
    async getTalk() {
      let {
        data: { content: title },
      } = await axios.get("https://api.uomg.com/api/rand.qinghua?format=json");

      let obj = {
        id: nanoid(),
        title,
      };

      this.talkList.push(obj);
    },
  },
});*/
import { reactive } from 'vue';

// pinia的组合式写法。各有千秋
export const useTalkStore = defineStore('talk', () => {
  // talkList相当于是state
  const talkList = reactive(JSON.parse(localStorage.getItem('talkList') as string) || []);

  async function getTalk() {
    let {
      data: { content: title },
    } = await axios.get('https://api.uomg.com/api/rand.qinghua?format=json');

    let obj = {
      id: nanoid(),
      title,
    };
    talkList.push(obj);
  }

  return {
    talkList,
    getTalk,
  };
});
